package com.yuncheng.spcyApi.utils;

import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;

public class DateConvert implements Converter{

    public Object convert(Class arg0, Object arg1) {

        if(arg1 instanceof String){
            String p = (String)arg1;
            if(p== null || p.trim().length()==0){
                return null;
            }
            try{
                if(p.matches("\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}")){
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return df.parse(p.trim());
                }else{
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    return df.parse(p.trim().split(" ")[0]);
                }
            }
            catch(Exception e){
                return null;
            }

        }else{
            return arg1;
        }

    }
}
